package de.jonasblatt.networkapi.rpi.network.server;

import java.util.Queue;
import java.util.TimerTask;
import java.util.concurrent.ConcurrentLinkedQueue;

import de.jonasblatt.networkapi.rpi.network.commands.ClientCommand;

public class ServerSchedule extends TimerTask {

	private Queue<ClientCommand> commandQueue;

	private Server server;

	public ServerSchedule(Server server) {
		this.server = server;

		this.commandQueue = new ConcurrentLinkedQueue<ClientCommand>();
	}

	/**
	 *
	 * @return the associated Server
	 */
	public Server getServer() {
		return this.server;
	}

	/**
	 * Add a command to the commandqueue
	 *
	 * @param cmd
	 */
	public void offerCommand(ClientCommand cmd) {
		this.commandQueue.offer(cmd);
	}

	/**
	 * If there exist any Commands in the commandQueue, the command is executed
	 */
	@Override
	public void run() {
		while (!this.commandQueue.isEmpty()) {
			ClientCommand cmd = this.commandQueue.poll();
			System.out.println("Execute Command from Client");
			cmd.execute(this.server);
		}

	}

}
